package com.a2v2.online.store.common.util;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import com.a2v2.online.store.common.constant.ServiceConstant;

/**
 * @author AurionPro.
 *
 */
public class CommonDateUtil {

    /**
     * This method is used to return TXN date + biller specific settlement days.
     * @param pBillerSettlementDays settlement days in integer value.
     * @return date time
     */
    public static Timestamp getCreditedOnDate(final int pBillerSettlementDays) {
        Calendar aTxnDatePlusN = Calendar.getInstance();
        /**
         * T+N days
         */
        aTxnDatePlusN.add(Calendar.DATE, pBillerSettlementDays);
        return new Timestamp(aTxnDatePlusN.getTime().getTime());
    }
    /**
     * Subtract days from due date.
     * This method is used to return TXN date - biller specific due date.
     * @param pBillerDueDate days in integer value.
     * @param pDueDate Due date
     * @return date time
     */
    public static Timestamp getPaymentDueDate(final Date pDueDate , final int pBillerDueDate) {
        SimpleDateFormat aDateFormat = new SimpleDateFormat("dd-MM-yyyy");
        Date aDate = null;
        try {
            aDate = aDateFormat.parse(aDateFormat.format(pDueDate));
        }
        catch (ParseException pParseEx) {
            pParseEx.printStackTrace();
        }
        Calendar aCalendar = Calendar.getInstance();
        aCalendar.setTime(aDate);
        Calendar aPaymentDueDateMinuN = aCalendar;
        aPaymentDueDateMinuN.add(Calendar.DATE, -pBillerDueDate);
        return new Timestamp(aPaymentDueDateMinuN.getTime().getTime());
    }
    /**
     * The below method is going to parse the string into date in a given pattern and will return Date Object.  
     */
    
    public static Date formatStringtoDate(final String date)
    {
    	String[] pattern = {"MM/dd/yyyy","dd/MM/yyyy","dd-MMM-yyyy"};
    	Date parserDate = null;
    	try {
				//TODO parserDate = DateUtils.parseDate(date, pattern);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	return parserDate;
    }
    
    /**
     * This method converts sql time stamp to formated date as string in default date format.
     * @param timestamp
     * @return the fmtDate
     */
    public static String getFmtDateFrmTimeStamp(final Timestamp timestamp)
    {
    	String fmtDate = null;
    	Date date = null;
    	try {
    		if (timestamp != null){
    			date = new Date(timestamp.getTime());
    			SimpleDateFormat aDateFormat = new SimpleDateFormat(ServiceConstant.DEFAULT_DATE_PATTERN);
    			fmtDate = aDateFormat.format(date);
    		}
    	} catch (Exception e) {
    		e.printStackTrace();
    	}
    	return fmtDate;
    } 
    
    /**
     * This method returns the cut of date time from due date and cut off days.
     * @param dueDateTS
     * @return the cutOffDtTime
     */
    public static long getcutOffDateTime(final Timestamp dueDateTS, Integer cutOffDays)
    {
    	long cutOffDtTime = 0;    	
    	try {
			// due date calculation				
			long dueDateTime = dueDateTS.getTime();
			Date dueDate = new Date(dueDateTime);
			Calendar dueDateCal = Calendar.getInstance();
	        dueDateCal.setTime(dueDate);
	        
	        // cut off date calculation
	        Calendar cutOffDateDateCal = dueDateCal;
	        cutOffDateDateCal.add(Calendar.DATE, -(cutOffDays));
	        cutOffDtTime = cutOffDateDateCal.getTime().getTime();

    	} catch (Exception e) {
    		e.printStackTrace();
    	}
    	return cutOffDtTime;
    }
    
    /**
     * This method converts date id string format into sql time stamp db default date format.
     * @param strDate
     * @return the timeStamp
     */
    public static Timestamp getTimeStampFromStrDate(final String strDate)
    {
    	Date date = null;
    	Timestamp timeStamp = null;
    	try {
    		SimpleDateFormat dateFormat = new SimpleDateFormat(ServiceConstant.DB_DATE_PATTERN);    		
    		if (strDate != null){
    			date = dateFormat.parse(strDate);
    			timeStamp = new Timestamp(date.getTime());
    		}
    	} catch (Exception e) {
    		e.printStackTrace();
    	}
    	return timeStamp;
    }
    
    /**
     * The below method is going to parse the string into date in a given pattern and will return Date Object.  
     */    
    public static String getFormatedDate(final String date)
    {
    	Date parserDate = null;
    	String fmtDate = null;
    	try {
    		String[] pattern = {"dd-MMM-yyyy"};
    		//TODO parserDate = DateUtils.parseDate(date, pattern);
    		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
    		fmtDate = formatter.format(parserDate);    		    		
    	} catch (Exception e) {
    		e.printStackTrace();
    	}
    	return fmtDate;
    }
    
    public static void main(String ar[]) {
    	System.out.println("Date is " +getFormatedDate("12-Dec-2012"));
    }

}
